﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Se7en.Admin.Model;
using ERMSys.Framework.Utilities;

namespace Se7en.Admin.Business
{
    partial class AccountingBLL
    {

        public static Role Role_GetRoleById(this AdminContext context, int roleId)
        {
            return context.Roles.FirstOrDefault(e => e.RoleID == roleId);
        }

        public static void Role_AddNew(this AdminContext context, Role role)
        {
            context.AddToRoles(role);
        }

        public static bool Role_CheckDuplicateRole(this AdminContext context, int roleType, string name)
        {
            Role obj = context.Roles.FirstOrDefault(e => e.Type == roleType && e.Name.ToLower() == name.ToLower());
            return obj != null;
        }

        public static bool Role_CheckExistedRole(this AdminContext context, int roleId, int roleType, string name)
        {
            Role obj = context.Roles.FirstOrDefault(e => e.RoleID != roleId && e.Type == roleType && e.Name.ToLower() == name.ToLower());
            return obj != null;
        }

        public static List<Role> Role_GetAll(this AdminContext context, int roleType, string name, int pageIndex, int pageSize, out int total)
        {
            if (string.IsNullOrEmpty(name))
                return context.Roles.Where(e => e.Type == roleType).OrderBy(e => e.Name).ToListPaging(pageIndex, pageSize, out total);
            return context.Roles.Where(e => e.Type == roleType && e.Name.ToLower().Contains(name.ToLower())).OrderBy(e => e.Name).ToListPaging(pageIndex, pageSize, out total);
        }

        public static void Role_UpdateDefaultRole(this AdminContext context, int roleId)
        {
            Role defaultRole = context.Role_GetRoleById(roleId);
            if (defaultRole != null)
            {
                Role currentDefault = context.Roles.FirstOrDefault(e => e.Type == (int)RoleType.Admin && e.IsDefault);
                currentDefault.IsDefault = false;
                defaultRole.IsDefault = true;
                context.Update(currentDefault);
                context.Update(defaultRole);
            }
        }

        public static void Role_Delete(this AdminContext context, int roleId)
        {
            context.DeleteAllObject(context.UserRoles.Where(e => e.RoleID == roleId).ToList());
            //context.DeleteAllObject(context.Permissions.Where(e => e.RoleId == roleId).ToList());
            context.Roles.DeleteObject(context.Roles.FirstOrDefault(e => e.RoleID == roleId));
        }

        public static void Role_AssignToUserId(this AdminContext context, List<int> roleIdList, int userId)
        {
            context.Role_RemoveAllRoleOfUserId(userId);
            UserRole userRole = null;
            foreach (int roleId in roleIdList)
            {
                userRole = new UserRole() { UserID = userId, RoleID = roleId };
                context.UserRoles.AddObject(userRole);
            }
        }

        public static void Role_AssignAllRoleToUserId(this AdminContext context, int userId)
        {
            context.Role_RemoveAllRoleOfUserId(userId);
            UserRole userRole =
                userRole = new UserRole() { UserID = userId };
            context.UserRoles.AddObject(userRole);
        }

        public static void Role_RemoveRoleOfUserId(this AdminContext context, List<int> roleIdList, int userId)
        {
            context.DeleteAllObject(context.UserRoles.Where(e => e.UserID == userId && roleIdList.Contains(e.RoleID.Value)));
        }

        public static void Role_RemoveAllRoleOfUserId(this AdminContext context, int userId)
        {
            context.DeleteAllObject(context.UserRoles.Where(e => e.UserID == userId));
        }

    }
}
